package com.itkim.qian.service.impl;

import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.itkim.YZUserInfo.domain.YzUser;
import com.itkim.judge.domain.YzJudge;
import com.itkim.judge.domain.YzJudgeRes;
import com.itkim.qian.controller.LoginController;
import com.itkim.qian.domain.YzUserRes;
import com.itkim.qian.domain.YzWorkR;
import com.itkim.qian.mapper.LoginMapper;
import com.itkim.qian.service.LoginService;
import com.itkim.qian.utils.JWTUtil;
import com.itkim.work.domain.YzFile;
import com.itkim.work.domain.YzWorkRes;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public AjaxResult login(YzUserRes yzUserRes) throws Exception {
        YzUserRes login = loginMapper.login(yzUserRes);
        Map<String,String> map = new HashMap<>();
        map.put("userId",login.getUserId().toString());
        map.put("userName",login.getUserName());
        map.put("name",login.getName());
        map.put("permission",login.getPermission());
        map.put("school",login.getSchool());
        map.put("group",login.getGroup());
        map.put("avatar",login.getAvatar());
        String token = JWTUtil.getToken(map);
        return AjaxResult.success(token);
    }

    @Override
    public int updatePassword(String oldPassword, String newPassword, YzUserRes yzUserRes) {
        YzUserRes yzUserRes1 = loginMapper.selectYzUserResByUserId(yzUserRes.getUserId());

        if (!oldPassword.equals(yzUserRes1.getPassword()))
            return -1;

        return loginMapper.updatePassword(newPassword,yzUserRes.getUserId());
    }

    @Autowired
    private SnowflakeGenerator snowflakeGenerator;

    @Override
    public String baoming(Long userId, YzWorkRes yzWorkRes) {

        yzWorkRes.setWorkId(snowflakeGenerator.next());
        yzWorkRes.setCreateTime(DateUtils.getNowDate());

        loginMapper.baoming(userId,yzWorkRes);

        return yzWorkRes.getWorkId().toString();
    }

    @Override
    public void export(Long workId, Integer group, MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        YzFile yzFile = new YzFile();
        yzFile.setFileName(filename);
        String path = filename.endsWith(".pdf") ? "D:\\WorkSpace\\JAVA\\RuoYi\\file\\" : "D:\\WorkSpace\\JAVA\\RuoYi\\file\\video\\";
        String prefix = UUID.randomUUID().toString().replaceAll("-","") + filename.substring(filename.indexOf('.'));
        File file1 = new File(path + prefix);
        file.transferTo(file1);
        String address = filename.endsWith(".pdf") ? "http://localhost/file/" : "http://localhost/file/video/";
        yzFile.setFileAddress(address + prefix);
        yzFile.setFileId(snowflakeGenerator.next());
        yzFile.setCreateTime(DateUtils.getNowDate());

        loginMapper.export(workId,group,yzFile);
    }


    @Override
    public List<YzWorkR> list(YzWorkR yzWorkR) {
        return loginMapper.list(yzWorkR);
    }

    @Override
    public int judge(YzJudgeRes yzJudgeRes) {
        yzJudgeRes.setScore(yzJudgeRes.getJudgeCourse() +
                yzJudgeRes.getJudgePlan() +
                yzJudgeRes.getJudgePro() +
                yzJudgeRes.getJudgeSelect() +
                yzJudgeRes.getJudgeTeach() +
                yzJudgeRes.getJudgeVideo());
        yzJudgeRes.setCreateTime(DateUtils.getNowDate());
        yzJudgeRes.setJudgeId(snowflakeGenerator.next());
        int i = loginMapper.insertJudge(yzJudgeRes);
        if (i < 0)
            return i;

        List<YzJudgeRes> list = loginMapper.selectYzJudgeByWorkId(yzJudgeRes.getWorkId());
        double sum = 0;
        for (YzJudgeRes judgeRes : list) {
            sum += yzJudgeRes.getScore();
        }
        sum /= list.size();

        i = loginMapper.updateScore(yzJudgeRes.getWorkId(),sum);
        return i;
    }
}
